package com.hfjava.practice04;

/**
 * @author Administrator
 * @version V1.0
 * @ProjectName:IntelliJ IDEA
 * @PackageName: com.hfjava.practice04
 * @ClassName: AccountDeposit
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @date 2019/12/27 9:15
 * @Copyright 大牛版板所有
 */
public class AccountDeposit implements Runnable {

    private Account account = new Account();

    @Override
    public void run() {
        for (int i = 1; i <= 3; i++) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            makeDeposit(100);
        }
        System.out.println("每人最多存三次钱！");
    }

    private void makeDeposit(int num){
        synchronized (account){
            if (num == 100){
                int totalMoney = account.deposit();
                System.out.println(Thread.currentThread().getName()+"取款"+num+"元，账户总金额为："+totalMoney+"元");
            }
            else {
                System.out.println("每次存入的金额必须为100元");
            }
        }
    }


    public static void main(String[] args) {
        AccountDeposit a = new AccountDeposit();
        Thread thread1 = new Thread(a,"储户张三");
        Thread thread2 = new Thread(a,"储户王五");

        thread1.start();
        thread2.start();
    }
}
